package org.jivesoftware.smack;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.jivesoftware.smack.packet.Packet;

class PacketWriter
{
  private XMPPConnection connection;
  private boolean done;
  private Thread keepAliveThread;
  private long lastActive = System.currentTimeMillis();
  private final BlockingQueue<Packet> queue = new ArrayBlockingQueue(500, true);
  private Writer writer;
  private Thread writerThread;

  protected PacketWriter(XMPPConnection paramXMPPConnection)
  {
    this.connection = paramXMPPConnection;
    init();
  }

  // ERROR //
  private Packet nextPacket()
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore_1
    //   2: aload_0
    //   3: getfield 52	org/jivesoftware/smack/PacketWriter:done	Z
    //   6: ifne +20 -> 26
    //   9: aload_0
    //   10: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   13: invokeinterface 78 1 0
    //   18: checkcast 80	org/jivesoftware/smack/packet/Packet
    //   21: astore_1
    //   22: aload_1
    //   23: ifnull +5 -> 28
    //   26: aload_1
    //   27: areturn
    //   28: aload_0
    //   29: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   32: astore_3
    //   33: aload_3
    //   34: monitorenter
    //   35: aload_0
    //   36: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   39: invokevirtual 83	java/lang/Object:wait	()V
    //   42: aload_3
    //   43: monitorexit
    //   44: goto -42 -> 2
    //   47: astore 4
    //   49: aload_3
    //   50: monitorexit
    //   51: aload 4
    //   53: athrow
    //   54: astore_2
    //   55: goto -53 -> 2
    //
    // Exception table:
    //   from	to	target	type
    //   35	51	47	finally
    //   28	35	54	java/lang/InterruptedException
    //   51	54	54	java/lang/InterruptedException
  }

  // ERROR //
  private void writePackets(Thread paramThread)
  {
    // Byte code:
    //   0: aload_0
    //   1: invokevirtual 90	org/jivesoftware/smack/PacketWriter:openStream	()V
    //   4: aload_0
    //   5: getfield 52	org/jivesoftware/smack/PacketWriter:done	Z
    //   8: ifne +15 -> 23
    //   11: aload_0
    //   12: getfield 92	org/jivesoftware/smack/PacketWriter:writerThread	Ljava/lang/Thread;
    //   15: astore 12
    //   17: aload 12
    //   19: aload_1
    //   20: if_acmpeq +67 -> 87
    //   23: aload_0
    //   24: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   27: astore 9
    //   29: aload 9
    //   31: monitorenter
    //   32: aload_0
    //   33: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   36: invokeinterface 96 1 0
    //   41: ifeq +133 -> 174
    //   44: aload_0
    //   45: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   48: invokevirtual 101	java/io/Writer:flush	()V
    //   51: aload 9
    //   53: monitorexit
    //   54: aload_0
    //   55: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   58: invokeinterface 104 1 0
    //   63: aload_0
    //   64: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   67: ldc 106
    //   69: invokevirtual 110	java/io/Writer:write	(Ljava/lang/String;)V
    //   72: aload_0
    //   73: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   76: invokevirtual 101	java/io/Writer:flush	()V
    //   79: aload_0
    //   80: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   83: invokevirtual 113	java/io/Writer:close	()V
    //   86: return
    //   87: aload_0
    //   88: invokespecial 115	org/jivesoftware/smack/PacketWriter:nextPacket	()Lorg/jivesoftware/smack/packet/Packet;
    //   91: astore 13
    //   93: aload 13
    //   95: ifnull -91 -> 4
    //   98: aload_0
    //   99: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   102: astore 14
    //   104: aload 14
    //   106: monitorenter
    //   107: aload_0
    //   108: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   111: aload 13
    //   113: invokevirtual 119	org/jivesoftware/smack/packet/Packet:toXML	()Ljava/lang/String;
    //   116: invokevirtual 110	java/io/Writer:write	(Ljava/lang/String;)V
    //   119: aload_0
    //   120: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   123: invokevirtual 101	java/io/Writer:flush	()V
    //   126: aload_0
    //   127: invokestatic 34	java/lang/System:currentTimeMillis	()J
    //   130: putfield 36	org/jivesoftware/smack/PacketWriter:lastActive	J
    //   133: aload 14
    //   135: monitorexit
    //   136: goto -132 -> 4
    //   139: astore 15
    //   141: aload 14
    //   143: monitorexit
    //   144: aload 15
    //   146: athrow
    //   147: astore_2
    //   148: aload_0
    //   149: getfield 52	org/jivesoftware/smack/PacketWriter:done	Z
    //   152: ifne -66 -> 86
    //   155: aload_0
    //   156: iconst_1
    //   157: putfield 52	org/jivesoftware/smack/PacketWriter:done	Z
    //   160: aload_0
    //   161: getfield 45	org/jivesoftware/smack/PacketWriter:connection	Lorg/jivesoftware/smack/XMPPConnection;
    //   164: getfield 125	org/jivesoftware/smack/XMPPConnection:packetReader	Lorg/jivesoftware/smack/PacketReader;
    //   167: aload_2
    //   168: invokevirtual 131	org/jivesoftware/smack/PacketReader:notifyConnectionError	(Ljava/lang/Exception;)V
    //   171: goto -85 -> 86
    //   174: aload_0
    //   175: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   178: invokeinterface 134 1 0
    //   183: checkcast 80	org/jivesoftware/smack/packet/Packet
    //   186: astore 11
    //   188: aload_0
    //   189: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   192: aload 11
    //   194: invokevirtual 119	org/jivesoftware/smack/packet/Packet:toXML	()Ljava/lang/String;
    //   197: invokevirtual 110	java/io/Writer:write	(Ljava/lang/String;)V
    //   200: goto -168 -> 32
    //   203: astore 10
    //   205: aload 9
    //   207: monitorexit
    //   208: aload 10
    //   210: athrow
    //   211: astore_3
    //   212: aload_3
    //   213: invokevirtual 137	java/lang/Exception:printStackTrace	()V
    //   216: goto -162 -> 54
    //   219: astore 6
    //   221: aload_0
    //   222: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   225: invokevirtual 113	java/io/Writer:close	()V
    //   228: goto -142 -> 86
    //   231: astore 7
    //   233: goto -147 -> 86
    //   236: astore 4
    //   238: aload_0
    //   239: getfield 60	org/jivesoftware/smack/PacketWriter:writer	Ljava/io/Writer;
    //   242: invokevirtual 113	java/io/Writer:close	()V
    //   245: aload 4
    //   247: athrow
    //   248: astore 8
    //   250: goto -164 -> 86
    //   253: astore 5
    //   255: goto -10 -> 245
    //
    // Exception table:
    //   from	to	target	type
    //   107	144	139	finally
    //   0	17	147	java/io/IOException
    //   23	32	147	java/io/IOException
    //   54	63	147	java/io/IOException
    //   79	86	147	java/io/IOException
    //   87	107	147	java/io/IOException
    //   144	147	147	java/io/IOException
    //   208	211	147	java/io/IOException
    //   212	216	147	java/io/IOException
    //   221	228	147	java/io/IOException
    //   238	245	147	java/io/IOException
    //   245	248	147	java/io/IOException
    //   32	54	203	finally
    //   174	208	203	finally
    //   23	32	211	java/lang/Exception
    //   208	211	211	java/lang/Exception
    //   63	79	219	java/lang/Exception
    //   221	228	231	java/lang/Exception
    //   63	79	236	finally
    //   79	86	248	java/lang/Exception
    //   238	245	253	java/lang/Exception
  }

  void cleanup()
  {
    this.connection.interceptors.clear();
    this.connection.sendListeners.clear();
  }

  protected void init()
  {
    this.writer = this.connection.writer;
    this.done = false;
    this.writerThread = new Thread(this)
    {
      public void run()
      {
        PacketWriter.access$4(this.this$0, this);
      }
    };
    this.writerThread.setName("Smack Packet Writer (" + this.connection.connectionCounterValue + ")");
    this.writerThread.setDaemon(true);
  }

  void openStream()
    throws IOException
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<stream:stream");
    localStringBuilder.append(" to=\"").append(this.connection.getServiceName()).append("\"");
    localStringBuilder.append(" xmlns=\"jabber:client\"");
    localStringBuilder.append(" xmlns:stream=\"http://etherx.jabber.org/streams\"");
    localStringBuilder.append(" version=\"1.0\">");
    this.writer.write(localStringBuilder.toString());
    this.writer.flush();
  }

  // ERROR //
  public void sendPacket(Packet paramPacket)
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 52	org/jivesoftware/smack/PacketWriter:done	Z
    //   4: ifne +45 -> 49
    //   7: aload_0
    //   8: getfield 45	org/jivesoftware/smack/PacketWriter:connection	Lorg/jivesoftware/smack/XMPPConnection;
    //   11: aload_1
    //   12: invokevirtual 204	org/jivesoftware/smack/XMPPConnection:firePacketInterceptors	(Lorg/jivesoftware/smack/packet/Packet;)V
    //   15: aload_0
    //   16: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   19: aload_1
    //   20: invokeinterface 208 2 0
    //   25: aload_0
    //   26: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   29: astore_3
    //   30: aload_3
    //   31: monitorenter
    //   32: aload_0
    //   33: getfield 43	org/jivesoftware/smack/PacketWriter:queue	Ljava/util/concurrent/BlockingQueue;
    //   36: invokevirtual 211	java/lang/Object:notifyAll	()V
    //   39: aload_3
    //   40: monitorexit
    //   41: aload_0
    //   42: getfield 45	org/jivesoftware/smack/PacketWriter:connection	Lorg/jivesoftware/smack/XMPPConnection;
    //   45: aload_1
    //   46: invokevirtual 214	org/jivesoftware/smack/XMPPConnection:firePacketSendingListeners	(Lorg/jivesoftware/smack/packet/Packet;)V
    //   49: return
    //   50: astore_2
    //   51: aload_2
    //   52: invokevirtual 215	java/lang/InterruptedException:printStackTrace	()V
    //   55: goto -6 -> 49
    //   58: astore 4
    //   60: aload_3
    //   61: monitorexit
    //   62: aload 4
    //   64: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   15	25	50	java/lang/InterruptedException
    //   32	41	58	finally
    //   60	62	58	finally
  }

  void setWriter(Writer paramWriter)
  {
    this.writer = paramWriter;
  }

  public void shutdown()
  {
    this.done = true;
    synchronized (this.queue)
    {
      this.queue.notifyAll();
      return;
    }
  }

  void startKeepAliveProcess()
  {
    int i = SmackConfiguration.getKeepAliveInterval();
    if (i > 0)
    {
      KeepAliveTask localKeepAliveTask = new KeepAliveTask(this, i);
      this.keepAliveThread = new Thread(localKeepAliveTask);
      localKeepAliveTask.setThread(this.keepAliveThread);
      this.keepAliveThread.setDaemon(true);
      this.keepAliveThread.setName("Smack Keep Alive (" + this.connection.connectionCounterValue + ")");
      this.keepAliveThread.start();
    }
  }

  public void startup()
  {
    this.writerThread.start();
  }

  private class KeepAliveTask
  implements Runnable
  {
    private int delay;
    private Thread thread;

    public KeepAliveTask(, int paramInt)
    {
      this.delay = paramInt;
    }

    public void run()
    {
      label6: Writer localWriter;
      try
      {
        Thread.sleep(15000L);
        if ((PacketWriter.access$0(this.this$0)) || (PacketWriter.access$1(this.this$0) != this.thread))
          return;
        localWriter = PacketWriter.access$2(this.this$0);
        label91: monitorenter;
      }
      catch (InterruptedException localInterruptedException1)
      {
        try
        {
          int i;
          long l = System.currentTimeMillis() - PacketWriter.access$3(this.this$0);
          if (l >= i);
        }
        finally
        {
          try
          {
            PacketWriter.access$2(this.this$0).write(" ");
            PacketWriter.access$2(this.this$0).flush();
            monitorexit;
            try
            {
              Thread.sleep(this.delay);
            }
            catch (InterruptedException localInterruptedException2)
            {
              break label6:
              localObject = finally;
              monitorexit;
              throw localObject;
            }
            localInterruptedException1 = localInterruptedException1;
          }
          catch (Exception localException)
          {
            break label91:
          }
        }
      }
    }

    protected void setThread()
    {
      this.thread = paramThread;
    }
  }
}